package serv

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

func AdminOrgIndexCtrl(c *gin.Context) {
	if c.Request.Method == "POST" {
		param := GetJsonData(c)

		var adminOrgArr []map[string]any
		where := "pid=0"

		if param["name"] != nil {
			where += " and name like '%%" + param["name"].(string) + "%%'"
		}
		if param["username"] != nil {
			where += " and username like '%%" + param["username"].(string) + "%%'"
		}

		dbEngine.Table("f_org").Select("id,name,type,path,status,icon,pid,sort").Where(where).OrderBy("sort asc").Find(&adminOrgArr)
		cnt, _ := dbEngine.Table("f_org").Where(where).Count()

		for index, v := range adminOrgArr {
			var tmpArr []map[string]any
			dbEngine.Table("f_org").Select("id,name,type,path,status,icon,pid,sort").Where("pid=?", v["id"]).OrderBy("sort asc").Find(&tmpArr)
			adminOrgArr[index]["children"] = tmpArr
			fmt.Println(index, v)
		}

		c.JSON(200, ApiList{
			Status: 200,
			Data: ApiDataListObj{
				Total:       cnt,
				PerPage:     0,
				CurrentPage: 0,
				LastPage:    0,
				Data:        adminOrgArr,
			},
		})
	} else {
		c.HTML(http.StatusOK, "admin-org-index.html", nil)
	}
}

func AdminOrgGetFieldListCtrl(c *gin.Context) {
	if c.Request.Method == "POST" {

		var adminOrgArr []map[string]any
		where := "pid=0"

		dbEngine.Table("f_org").Select("id, id as val,name as `key`").Where(where).OrderBy("sort asc").Find(&adminOrgArr)

		for index, v := range adminOrgArr {
			var tmpArr []map[string]any
			dbEngine.Table("f_org").Select("id as val,name as `key`").Where("pid=?", v["id"]).OrderBy("sort asc").Find(&tmpArr)
			if len(tmpArr) > 0 {
				adminOrgArr[index]["children"] = tmpArr
			} else {
				adminOrgArr[index]["children"] = nil
			}
			fmt.Println(index, v)
		}

		c.JSON(200, map[string]any{"status": 200, "data": adminOrgArr})
	} else {
		c.HTML(http.StatusOK, "admin-org-index.html", nil)
	}
}

func AdminOrgGetUpdateInfoCtrl(c *gin.Context) {
	param := GetJsonData(c)
	id := param["id"]

	orgArr := map[string]any{}
	dbEngine.Table("f_org").Select("id,name,type,path,status,icon,pid,sort").Where("id=?", id).Get(&orgArr)

	c.JSON(200, map[string]any{"status": 200, "data": orgArr})
}

func AdminOrgUpdateCtrl(c *gin.Context) {
	param := GetJsonData(c)
	if param["id"] == nil {
		dbEngine.Table("f_org").Insert(param)
		c.JSON(200, map[string]any{"status": 200, "msg": "添加成功"})
	} else {
		dbEngine.Table("f_org").Where("id=?", param["id"]).Update(param)
		c.JSON(200, map[string]any{"status": 200, "msg": "更新成功"})
	}
}

func AdminOrgDeleteCtrl(c *gin.Context) {
	param := GetJsonData(c)

	if param["id"] != nil {
		dbEngine.Table("f_org").Where("id=?", param["id"]).Delete()
		c.JSON(200, map[string]any{"status": 200, "msg": "删除成功"})
	}
}

func AdminOrgUpdateExtCtrl(c *gin.Context) {
	param := GetJsonData(c)

	if param["id"] != nil {
		if param["status"] != nil {
			dbEngine.Table("f_org").Where("id=?", param["id"]).Update(map[string]any{"status": param["status"]})
		} else if param["sort"] != nil {
			dbEngine.Table("f_org").Where("id=?", param["id"]).Update(map[string]any{"sort": param["sort"]})
		}

		c.JSON(200, map[string]any{"status": 200, "msg": "更新成功"})
	} else {
		c.JSON(200, map[string]any{"status": 200, "msg": "ID出错"})
	}
}
